/*
 * CodeSaveCommand.java
 *
 * Created on 2006년 3월 29일 (수), 오후 4:11
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.qbooks.cms.product;

import java.io.File;
import java.util.*;




import javax.servlet.*;
import javax.servlet.http.*;

import com.zll.framework.conf.Configuration;
import com.zll.framework.db.DBManager;
import com.zll.framework.model.HashBox;
import com.zll.framework.model.HashList;
import com.zll.framework.servlet.Command;
import com.zll.framework.util.ZipUtil;
import com.qbooks.common.CommonUtil;





/**
 *
 * @author eastar
 */
public class BookSaveCommand implements Command
{
    public String execute(HttpServletRequest  request, HttpServletResponse response)
    throws Exception
    {
    	HashBox box = new HashBox(request);

    	String storage	= Configuration.get("dir.storage");
		String resUrl	= Configuration.getHomePath() + File.separator + "store" + File.separator + "res" + File.separator + box.get("publisher_id");
    	String dbPath 	= File.separator + "db" + File.separator + box.get("publisher_id");
    	String orgPath 	= File.separator + "org" + File.separator + box.get("publisher_id");

		////////////////////////// 웹 경로에 저장 ////////////////////////
		//표지이미지 처리
		File f = box.getFile("book_image_url");
		if (f != null) {
			File dir = new File(resUrl);
			if (!dir.exists()) {
				dir.mkdir();
			}
			File newFile = new File(dir.getPath() + File.separator + box.get("book_code") + "c" + f.getName().substring(f.getName().lastIndexOf('.')).toLowerCase());
			f.renameTo(newFile);
			box.put("book_image_url", "http://" + Configuration.get("domain") + "/store/res/"+ box.get("publisher_id") + "/" + newFile.getName());
		}

		//소개 이미지
		f = box.getFile("intro_image_url");
		if (f != null) {
			File dir = new File(resUrl);
			if (!dir.exists()) {
				dir.mkdir();
			}
			File newFile = new File(dir.getPath() + File.separator + box.get("book_code") + "v" + f.getName().substring(f.getName().lastIndexOf('.')).toLowerCase());
			f.renameTo(newFile);
			box.put("intro_image_url", "http://" + Configuration.get("domain") + "/store/res/"+ box.get("publisher_id") + "/" + newFile.getName());
		}


		//////////////////////////서버 경로에 저장 ////////////////////////
		//큐북 원본
		f = box.getFile("book_path_org");
		if (f != null) {
			File dir = new File(storage + orgPath);
			if (!dir.exists()) {
				dir.mkdir();
			}
			File newFile = new File(dir.getPath() + File.separator + box.get("book_code") + ".qbook");
			f.renameTo(newFile);
			box.put("file_size", String.valueOf(newFile.length()));
			box.put("book_header", CommonUtil.getQBookHeader(newFile.getAbsolutePath()));
			box.put("book_path_org", orgPath + File.separator + newFile.getName());
		}
			
		//큐북 미리보기 원본
		f = box.getFile("preview_book_path");
		if (f != null) {
			File dir = new File(storage + orgPath);
			if (!dir.exists()) {
				dir.mkdir();
			}
			File newFile = new File(dir.getPath() + File.separator + box.get("book_code") + "s.qbook");
			f.renameTo(newFile);
			box.put("preview_file_size", String.valueOf(newFile.length()));
			box.put("preview_book_path", orgPath + File.separator + newFile.getName());
		}
				
				
		//큐북DB 안드로이드
		f = box.getFile("book_path_android");
		if (f != null) {
			File dir = new File(storage + dbPath);
			if (!dir.exists()) {
				dir.mkdir();
			}
			File newFile = new File(dir.getPath() + File.separator + box.get("book_code") + "-android.qdb");
			f.renameTo(newFile);
			box.put("book_path_android", dbPath + File.separator + newFile.getName());
		}


		//큐북DB 아이폰
		f = box.getFile("book_path_ios");
		if (f != null) {
			File dir = new File(storage + dbPath);
			if (!dir.exists()) {
				dir.mkdir();
			}
			File newFile = new File(dir.getPath() + File.separator + box.get("book_code") + "-ios.qdb");
			f.renameTo(newFile);
			box.put("book_path_ios", dbPath + File.separator + newFile.getName());
		}

	
		ProductDAO dao = new ProductDAO();
		dao.saveBook(box);

		HashMap result		= new HashMap();
		result.put("result", 	"true");
		result.put("target",	box.target);


		if (box.get("job_type").equals("U")) {
			result.put("action",	"./bookDetail.jsp?book_code="+box.get("book_code"));
		} else {
			result.put("action",	"./bookList.jsp");
		}

    	request.setAttribute("result", result);


    	return "/common/move_view.jsp";
    }
}
